package org.jradweb.tools.generator.maven;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.jradweb.tools.generator.freemarker.ClassGenerator;
import org.jradweb.tools.generator.xml.HibernateConfigOperator;
import org.jradweb.tools.generator.xml.SpringDaoConfigOperator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 * creade model class, dao class ,test class
 * add bean to spring, mapping to hibernate
 *
 * @goal createModel
 * 
 */
public class CreateModelMojo
    extends AbstractMojo
{
	/**
	 * @parameter expression="${project.build.directory}"
	 */	
	private File buildPath;
	
	/**
	 * @parameter expression="${basedir}"
	 */	
	private File baseDir;
	
	/**
	 *  @parameter expression="${project}"
	 */
	private MavenProject project;
	
	/**
	 * @parameter expression="${project.groupId}"
	 */
	private String groupId;
	
	/**
	 * @parameter expression="${project.artifactId}"
	 */
	private String artifactId;
	
    public void execute()
        throws MojoExecutionException
    {
    	String modelName=System.getProperty("model");
        
    	getLog().info("Create model...");
        getLog().info("groupId: "+groupId);
        getLog().info("artifactId: "+artifactId);
        getLog().info("model name: "+modelName);
        getLog().info("rootPath: "+baseDir.getAbsolutePath());
        
       
        try {
			getLog().info("baseDir="+this.baseDir.getAbsolutePath());
			getLog().info("project="+this.project);
			getLog().info("buildDir="+this.buildPath);
			
			//add model class and hibernate mapping config
			ClassGenerator cg=new ClassGenerator(this.baseDir,this.groupId,this.artifactId,this.getLog());
			cg.createModelClass(modelName);
			HibernateConfigOperator hco=new HibernateConfigOperator(this.baseDir,this.groupId,this.artifactId,this.getLog());			
			hco.addClassMapping(modelName);
			
			//add dao and daoImpl class
			cg.createDao(modelName);
			cg.createDaoImpl(modelName);
			
			SpringDaoConfigOperator opr = new SpringDaoConfigOperator(this.baseDir, this.groupId,this.artifactId,this.getLog());
			opr.addDaoBean(modelName);
			
		} catch (Exception e) {
			throw new MojoExecutionException(e.getMessage(),e);
		} 
    }
    
    private File getSpringDaoFile()throws java.io.FileNotFoundException{
    	String filePath=this.baseDir.getAbsolutePath()+"/src/main/resources/spring/"+this.artifactId+"-dao.xml";
    	File file=new File(filePath);
    	if(!file.exists()){
    		throw new FileNotFoundException("File \""+filePath+"\" not exist.");
    	}
    	return file;
    }
    
    private Document getSpringDaoDocument() throws IOException, ParserConfigurationException, SAXException{
    	File file=this.getSpringDaoFile();
    	Document doc=javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
    	Element beans=doc.getDocumentElement();
    	NodeList beansList=beans.getChildNodes();
    	for(int i=0;i<beansList.getLength();i++){
    		Node bean=beansList.item(i);
    		Node beanId=bean.getAttributes().getNamedItem("id");
    		getLog().info(bean.getNodeName()+": "+beanId.getNodeName());
    	}
    	return null;
    }
}
